﻿using AutoMapper;
using AutoMapper.Configuration.Annotations;
using KayakMes.Check.Application.Input;
using KayakMes.Check.Application.OUT;
using KayakMes.Check.Domain;
using KayakMes.Common;
using KayakMes.Common.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.Check.Application.Version
{
    public class VersionService : IVersionService
    {
        private readonly BaseReposiotry<VersionModel> _versionReposiotry;
        private readonly IMapper _mapper;

        public VersionService(BaseReposiotry<VersionModel> versionReposiotry, IMapper mapper)
        {
            _versionReposiotry = versionReposiotry;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加版本
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddVersion(AddVersioninput input)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            VersionModel versionModel = new VersionModel()
            {
                Id = YitIdHelper.NextId(),
                VersionName = input.VersionName,

                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now.AddDays(1),
                
            };
            int result = _versionReposiotry.Add(versionModel);
            if(result>0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "添加成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "添加失败", Data = 0 };
            }
        }
        /// <summary>
        /// 删除版本
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> DeleteVersion(long Id)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            if (Id <= 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "参数错误", Data = 0 };
            }
            var model = _versionReposiotry.GetModel(Id);
            int result = _versionReposiotry.Delete(model);
            if (result > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "删除成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "删除失败", Data = 0 };
            }
        }

        /// <summary>
        /// 获取所有版本分页
        /// </summary>
        /// <param name="input">分页查询参数</param>
        /// <returns>分页结果</returns>
        public ApiPaging<VersionOut> GetAllVersion(GetVersionInput input)
        {
            try
            {
                // 获取基础查询
                var query = _versionReposiotry.GetAll();

                // 如果有版本名称查询条件，进行筛选
                if (!string.IsNullOrEmpty(input.VersionName))
                {
                    query = query.Where(x => x.VersionName.Contains(input.VersionName));
                }

                // 获取总记录数
                var total = query.Count();

                // 进行分页查询
                var list = query.Skip((input.PageIndex - 1) * input.PageSize)
                              .Take(input.PageSize)
                              .ToList();

                // 使用AutoMapper转换为输出对象
                var result = _mapper.Map<List<VersionOut>>(list);

                // 返回分页结果
                return new ApiPaging<VersionOut>
                {
                    Code = ApiEnums.Success,
                    Message = "查询成功",
                    Total = total,
                    Items = result,
                    PageIndex = input.PageIndex,
                    PageSize = input.PageSize
                };
            }
            catch (Exception ex)
            {
                return new ApiPaging<VersionOut>
                {
                    Code = ApiEnums.Fail,
                    Message = $"查询失败：{ex.Message}",
                    Total = 0,
                    Items = new List<VersionOut>(),
                    PageIndex = input.PageIndex,
                    PageSize = input.PageSize
                };
            }
        }
    }
}
